/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cgb.portal.bean;

import br.cgb.portal.bo.ActivationCodeBO;
import br.cgb.portal.bo.UserBO;
import br.cgb.portal.entity.ActivationCode;
import br.cgb.portal.entity.User;
import br.cgb.portal.entity.enumeration.Country;
import br.cgb.portal.util.EncryptionUtil;
import br.cgb.portal.util.SmtpUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.mail.MessagingException;

/**
 *
 * @author LUCAS
 */
@ManagedBean
@ViewScoped
public class RegistrationBean extends AppBaseBean {

    @EJB
    private UserBO userBO;
    @EJB
    private ActivationCodeBO activationCodeBO;
    private User user;
    private String emailConfirm;
    private String password;
    private String passwordConfirm;
    private static List<SelectItem> countryList;
    private boolean mailConfirmationSent;

    static {
        countryList = new ArrayList<SelectItem>();
        countryList.add(new SelectItem(Country.ARGENTINA, "Argentina"));
        countryList.add(new SelectItem(Country.BRAZIL, "Brasil"));
        countryList.add(new SelectItem(Country.ENGLAND, "Inglaterra"));
        countryList.add(new SelectItem(Country.FRANCE, "França"));
        countryList.add(new SelectItem(Country.GERMANY, "Alemanha"));
        countryList.add(new SelectItem(Country.PORTUGAL, "Portugal"));
        countryList.add(new SelectItem(Country.SPAIN, "Espanha"));
        countryList.add(new SelectItem(Country.USA, "Estados Unidos"));
    }

    /**
     * Creates a new instance of RegistrationBean
     */
    public RegistrationBean() {
    }

    public void signup() {
        if (user.getEmail().equals(emailConfirm) && password.equals(passwordConfirm)) {
            user.setPassword(EncryptionUtil.encrypt(password));
            user.setValidated(Boolean.FALSE);
            userBO.create(user);
            ActivationCode activationCode = new ActivationCode(EncryptionUtil.generateActivationCode(), user);
            activationCodeBO.create(activationCode);
            try {
                SmtpUtil.sendEmail(user.getEmail(), activationCode.getCode(), getDOMAIN(), getBundle());
                mailConfirmationSent = true;
            } catch (MessagingException ex) {
                Logger.getLogger(RegistrationBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /*
     * Getters and setters
     */
    public User getUser() {
        return (user == null) ? user = new User() : user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getEmailConfirm() {
        return emailConfirm;
    }

    public void setEmailConfirm(String emailConfirm) {
        this.emailConfirm = emailConfirm;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    public List<SelectItem> getCountryList() {
        return countryList;
    }

    public boolean isMailConfirmationSent() {
        return mailConfirmationSent;
    }

    public void setMailConfirmationSent(boolean mailConfirmationSent) {
        this.mailConfirmationSent = mailConfirmationSent;
    }
}
